//
//  XLNewUserRecomVC.m
//  XiangLove
//
//  Created by 李传坤 on 2018/5/30.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLNewUserRecomVC.h"
#import "XLTabBarController.h"
#import "XLHostSideNewPersonCell.h"
#import <ReactiveCocoa.h>
#import "XLHTTPSessionManager.h"
#import "XLHTTPSessionManager+Camgirl.h"
#import "XLHostSideTuijianModel.h"
@interface XLNewUserRecomVC() <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>{
    
}
@property (nonatomic, strong) UIButton *loginButton;////马上进入

@property (nonatomic,strong)UICollectionView    *collectionView;
@property (nonatomic,strong)NSMutableArray      *modelArray;
@property (nonatomic,copy  )NSString            *page;
@property (nonatomic,copy  )NSString            *firstUpdata;


@end

@implementation XLNewUserRecomVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showBackButton:NULL];
    [self addCollectionView];
    if (@available(iOS 11.0, *)) {
        _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    if (self.navigationController.viewControllers.count > 1) {
        [self showBackButton:NULL];
    }
    self.navigationItem.title = @"推荐";
    self.page = @"1";
    [self requrstTalkedPeoplePage:self.page];
    
    self.loginButton = [[UIButton alloc]init];
    self.loginButton.frame = CGRectMake(35, _collectionView.frame.size.height + 60, SCREEN_WIDTH - 70, 50);
    self.loginButton.layer.cornerRadius = 4;
    self.loginButton.layer.masksToBounds = YES;
    self.loginButton.backgroundColor = RGB_HEX(0xFFBB01);
    [self.loginButton addTarget:self action:@selector(gotoMainVC) forControlEvents:UIControlEventTouchUpInside];
    [self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.loginButton setTitle:@"马上进入" forState:UIControlStateNormal];
    self.loginButton.titleLabel.font = [UIFont systemFontOfSize:18];
    self.loginButton.enabled = YES;
    [self.view addSubview:self.loginButton];
    //    [self MakeData];
    // Do any additional setup after loading the view.
}
- (void)gotoMainVC{
    XLTabBarController *mainVC = [[XLTabBarController alloc] init];
    [UIApplication sharedApplication].keyWindow.rootViewController = mainVC;
}
-(void)addCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 5;
    //flowLayout.headerReferenceSize = CGSizeMake(XQScreenW, 200);
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, XQScreenW, SCREEN_WIDTH+20) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.scrollEnabled = NO;
    _collectionView.backgroundColor = kWhiteColor;
    [_collectionView registerClass:[XLHostSideNewPersonCell class] forCellWithReuseIdentifier:NSStringFromClass([XLHostSideNewPersonCell class])];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderHost"];
    //[_collectionView registerClass:[XLUsersReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];

    [self.view addSubview:_collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
//    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
//    gradientLayer.colors = @[(id)RGBA(0, 0, 0, 0).CGColor, (id)RGBA(0, 0, 0, 0.5).CGColor];
//    gradientLayer.locations = @[@(0.5)];
//    gradientLayer.startPoint = CGPointMake(0.5, 1);
//    gradientLayer.endPoint = CGPointMake(0.5, 0);
//    gradientLayer.frame = CGRectMake(0, 0, ScreenWidth, 40);
//    [self.view.layer addSublayer:gradientLayer];
    
}
- (void)requrstTalkedPeoplePage:(NSString *)page
{
    //    self.userInfoModel = [[XLAccountManager shareInstance] model];
    NSDictionary *dic = @{@"pageIndex":_page,
                          @"pageSize":@"18",
                          @"userId":@"1220668",
                          @"type":@"2"
                          };
    [[[XLHTTPSessionManager sharedManager]XLV2GET:@"/api/v2/HomePage/GetHomePageList" parameters:dic resultClass:[XLHostSideTuijianModel class]] subscribeNext:^(id x) {
        NSLog(@"新人列表==%@",x);
        [MBProgressHUD hideHUDForView:self.view];
        self.collectionView.hidden = NO;
        NSArray * listAry = x;
        if (listAry.count > 0) {
            [self getJsonArray:listAry];
        }else{
        }
    } error:^(NSError *error) {
        NSLog(@"%@", error);
        [MBProgressHUD hideHUDForView:self.view];
        if (self.modelArray.count == 0) {
            //            self.promptView.hidden = NO;
            //            self.collectionView.hidden = YES;
        }
        
    }];
}
- (NSMutableArray *)modelArray
{
    if (_modelArray == nil) {
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}
- (void)getJsonArray:(NSArray *)array {
    if ([array isKindOfClass:[NSNull class]] || array.count == 0 || array == nil) {
        
    } else {
        if ([self.page isEqualToString:@"1"]) {
            [self.modelArray removeAllObjects];
        } else {
            
        }
        [self.modelArray addObjectsFromArray:array];
    }
    [self.collectionView reloadData];
}

#pragma mark - 下拉刷新
- (void)handelGCHeaderTableView {
    self.firstUpdata = @"1";
    self.page = @"1";
    [self requrstTalkedPeoplePage:self.page];
    //[self reshOnlinePeoplesData];
    
}

#pragma mark - 上拉加载
- (void)handelGCFootTableView {
    self.firstUpdata = @"1";
    self.page = [NSString stringWithFormat:@"%d", [self.page intValue] + 1];
    [self requrstTalkedPeoplePage:self.page];
}

#pragma mark - CollectionDelegate
#pragma mark -- 返回每个item的高度
//每个item的纵向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 5;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((XQScreenW-5)/3-3,(XQScreenW-5)/3-3);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    //    if (_lunboArray.count == 0) {
    //        return CGSizeMake(XQScreenW,0);
    //    }
    return CGSizeMake(XQScreenW,0);
    //    return CGSizeMake(XQScreenW,XQScreenW/3);
}

#pragma mark -- collectionView 的分组个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


#pragma mark -- item 的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_modelArray.count > 9) {
        return 9;
    }
    return self.modelArray.count;
    
}

#pragma mark -- cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XLHostSideTuijianModel * model = self.modelArray[indexPath.row];
    XLHostSideNewPersonCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XLHostSideNewPersonCell class]) forIndexPath:indexPath];
    cell.dataModels = model;
    cell.stateBgView.hidden = YES;
    cell.state.hidden = YES;
    cell.levelImage.hidden = YES;
    cell.nickNameLabel.hidden = YES;
    [cell.headerImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.contentView.mas_top).offset(2);
        make.left.equalTo(cell.contentView.mas_left).offset(0);
        make.right.equalTo(cell.contentView.mas_right).offset(0);
        make.bottom.equalTo(cell.contentView.mas_bottom).offset(-2);
        
    }];
    cell.backgroundColor = XLThemeColor;
    return cell;
}

#pragma mark -- 选中某个cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    [self analyticsEvent12];
    
    XLHostSideTuijianModel *model = self.modelArray[indexPath.row];
    //    XLPersonPageViewController *vc = [[XLPersonPageViewController alloc] init];
    //    vc.userId = model.UserId;
    //    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
